package com.mz.mattress.admin.service.impl;

import com.mz.mattress.admin.api.req.BlackSaveReq;
import com.mz.mattress.admin.api.resp.SaveDeviceResp;
import com.mz.mattress.admin.mapper.BlackItemMapper;
import com.mz.mattress.admin.mapper.BlackMapper;
import com.mz.mattress.admin.service.BlackItemService;
import com.mz.mattress.api.common.Result;
import com.mz.mattress.api.entity.Black;
import com.mz.mattress.api.entity.BlackItem;
import com.mz.mattress.api.util.StringTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.mz.mattress.api.common.Constants.TXT_SPLIT;

@Slf4j
@Service
public class BlackItemServiceImpl implements BlackItemService {

    @Autowired
    private BlackItemMapper blackItemMapper;

    @Autowired
    private BlackMapper blackMapper;

    @Override
    public Result<BlackItem> getById(Integer id) {
        BlackItem blackItem = blackItemMapper.selectById(id);
        if (blackItem == null) {
            return Result.fail("找不到广告");
        }
        return Result.ok(blackItem);
    }

    @Override
    public Result add(BlackItem blackItem) {
        blackItemMapper.insert(blackItem);
        return Result.ok();
    }

    @Override
    public Result<SaveDeviceResp> save(BlackSaveReq req) {

        String content = req.getContent();
        if (StringTools.isEmpty(content)) {
            return Result.fail("参数错误");
        }
        Integer id = req.getId();
        if (id == null) {
            return Result.fail("参数错误");
        }

        List<String> mobiles = blackItemMapper.list(id);
        Set<String> set = mobiles.stream().collect(Collectors.toSet());

        String[] list = content.split(TXT_SPLIT);

        int success = 0;
        int fail = 0;

        for (String mobile : list) {
            mobile = mobile.trim();

            if (!set.contains(mobile)) {
                BlackItem item = new BlackItem();
                item.setBlackId(id);
                item.setMobile(mobile);
                blackItemMapper.insert(item);
                ++success;
            } else {
                ++fail;
            }
        }

        Integer count = blackItemMapper.selectCount(id);
        Black update = new Black();
        update.setId(id);
        update.setCount(count);
        blackMapper.updateById(update);

        SaveDeviceResp resp = new SaveDeviceResp();
        resp.setSuccess(success);
        resp.setFail(fail);

        return Result.ok(resp);
    }

    @Override
    public Result updateById(BlackItem blackItem) {
        blackItemMapper.updateById(blackItem);
        return Result.ok();
    }

    @Override
    public Result deleteByIds(Set<Integer> ids) {
        blackItemMapper.deleteBatchIds(ids);
        return Result.ok();
    }

    public static void main(String[] args) {

        ArrayList<String> list = new ArrayList<>();

        Set<String> set = list.stream().collect(Collectors.toSet());

        System.out.println(set.size());

    }
}
